/*
 * $Id: HeroRoller.java 22 2010-06-13 14:47:01Z dave.terhune $
 *
 * Copyright (C) 2009, 2010 David Terhune
 *
 * This file is part of DiceRoller.
 *
 * DiceRoller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DiceRoller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with DiceRoller.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.dterhune.gaming.dice;

import android.app.Activity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import java.util.Random;

/** Rolls dice for Hero. */
@SuppressWarnings("serial") // Never actually serialized, so ignore warning.
class HeroRoller extends Random implements View.OnClickListener
{
    /** Number of dice per line in the output. */
    private static final int ITEMS_PER_LINE = 12;

    /** Result text field. */
    private TextView result;
    /** Sum text field. */
    private TextView sum;
    /** Number of dice text field. */
    private TextView xd6;
    /** Add a half die checkbox. */
    private CheckBox halfDie;
    /** Use killing damage checkbox. */
    private CheckBox killing;
    /** OCV text field. */
    private TextView ocv;

    /** Normal damage format string. */
    private String ndamage;
    /** Killing damage format string. */
    private String kdamage;
    /** To Hit roll format string. */
    private String hitRoll;

    /**
     * Creates a new HeroRoller.
     * @param activity The parent DiceRoller
     */
    public HeroRoller(Activity activity)
    {
        // Set the click listener
        View b = activity.findViewById(R.id.roll_hero3d);
        b.setOnClickListener(this);
        b = activity.findViewById(R.id.roll_heroxd);
        b.setOnClickListener(this);

        // Cache the text fields
        result = (TextView)activity.findViewById(R.id.hero_result);
        sum = (TextView)activity.findViewById(R.id.hero_sum);
        xd6 = (TextView)activity.findViewById(R.id.hero_xd6);
        halfDie = (CheckBox)activity.findViewById(R.id.hero_half_die);
        killing = (CheckBox)activity.findViewById(R.id.hero_killing);
        ocv = (TextView)activity.findViewById(R.id.hero_ocv);

        // Cache the string resources
        ndamage = activity.getString(R.string.ndamage);
        kdamage = activity.getString(R.string.kdamage);
        hitRoll = activity.getString(R.string.hitroll);
    }

    /**
     * Click handler.
     * @param v view that was clicked
     */
    public void onClick(View v)
    {
        // Figure out how many dice to roll
        int dice = 0;
        boolean half = false;
        boolean kill = false;
        boolean stun = true;
        switch(v.getId())
        {
            case R.id.roll_hero3d:
                dice = 3;
                stun = false;
                break;
            case R.id.roll_heroxd:
                dice = Integer.parseInt(xd6.getText().toString());
                half = halfDie.isChecked();
                kill = killing.isChecked();
                break;
        }

        int total = 0;
        int body = 0;
        StringBuilder sb = new StringBuilder();

        // Roll the dice
        for(int i = 0; i < dice; i++)
        {
            // Line wrap or add a comma
            if(i != 0 && (i % ITEMS_PER_LINE) == 0)
            {
                sb.append('\n');
            }
            else if(i != 0)
            {
                sb.append(", ");
            }

            int d = nextInt(6) + 1;
            total += d;
            sb.append(d);

            // Add body
            if(!kill)
            {
                switch(d)
                {
                    case 1: break; // no body
                    case 6: body += 2; break; // 2 body
                    default: body++; break; // 1 body
                }
            }
        }

        if(half)
        {
            // Line wrap or add a comma
            if(dice != 0 && (dice % ITEMS_PER_LINE) == 0)
            {
                sb.append("\n");
            }
            else if(dice != 0)
            {
                sb.append(", ");
            }

            // Add in the half die
            int d = nextInt(6);
            int val = (d + 2) / 2;
            total += val;
            sb.append(val);
            if(!kill && d >= 3)
            {
                body++; // add in the body
            } // add in the body
        }

        // Get stun multiplier (if necessary), and set sum text
        if(kill)
        {
            // Roll a stun multiplier die
            int stunx = nextInt(6);
            sum.setText(String.format(kdamage, total, stunx));
        }
        else if(stun)
        {
            // Damage roll
            sum.setText(String.format(ndamage, total, body));
        }
        else
        {
            // The naked 3d6 roll plus what DCV it hits
            try
            {
                int OCV = Integer.parseInt(ocv.getText().toString());
                sum.setText(String.format(hitRoll, total, 11 + OCV - total));
            }
            catch(NumberFormatException ex)
            {
                // There's an error in the OCV field.  Ignore it and just print
                // the result.
                sum.setText(Integer.toString(total));
            }
        }

        // Display the results
        result.setText(sb);
    }
}
